<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Resources\BookTypeResource;
use App\Models\BookType;
use Illuminate\Http\Request;

/***
 * 书籍分类
 * Class BookTypeController
 * @package App\Http\Controllers\Api\V1
 */
class BookTypeController extends ApiController
{
    public function list(Request $request, BookType $bookType)
    {
        $model = $bookType->newQuery();
        //获取单本目录
        if ($request->has('model_id') && !empty($request->model_id)) {
            $model->where('model_id', $request->model_id);
        }
        if ($request->has('pid')) {
            $model->where('pid', $request->input('pid', 0));
        } else {
            $model->where('pid', $request->input('pid', 0));
        }
        //排序
        if ($request->has('sort') && !empty($request->sort) && $request->sort == 1) {
            $model->orderBy('sort', 'asc');
        } else if ($request->has('sort') && !empty($request->sort) && $request->sort == 2) {
            $model->orderBy('sort', 'desc');
        }
        if (empty($request->input())) {
            $list = [];
        } else {
            $list = $model->where('status', 1)->paginate();
        }
        return $this->success('成功', BookTypeResource::collection($list));
    }
}
